//
//  VipInfoRecordTableViewCell.m
//  fitnessmanage
//
//  Created by wujian on 2017/10/29.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VipInfoRecordTableViewCell.h"

@implementation VipInfoRecordTableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.backgroundColor = CSClearColor;
        
        /**
         头像
         */
        UIImageView *headImageView = [[UIImageView alloc] init];
        headImageView.backgroundColor = CSRedColor;
        headImageView.contentMode = UIViewContentModeScaleAspectFill;
        headImageView.layer.masksToBounds = YES;
        headImageView.layer.cornerRadius = 20;
        [self.contentView addSubview:headImageView];
        self.headImageView = headImageView;
        
        UIImageView *whiteBgView = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageNamed:@"icon_talk"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(35, 30, 5,10) resizingMode:UIImageResizingModeStretch];
        whiteBgView.image = image;
        [self.contentView addSubview:whiteBgView];
        self.whiteBgView = whiteBgView;
        
        TTTAttributedLabel *detailLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        detailLabel.font = MidFont;
        detailLabel.numberOfLines = 0;
        detailLabel.lineBreakMode = NSLineBreakByCharWrapping;
        detailLabel.lineSpacing = 5;        
        detailLabel.text = @"";
        [whiteBgView addSubview:detailLabel];
        self.detailLabel = detailLabel;
        
        UIImageView *detailImageView = [[UIImageView alloc] init];
        [whiteBgView addSubview:detailImageView];
        detailImageView.contentMode = UIViewContentModeScaleAspectFill;
        detailImageView.layer.masksToBounds = YES;
        self.detailImageView = detailImageView;
        
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.text = @"2017-08-06 10：14";
        timeLabel.font = SmallFont;
        timeLabel.textColor = CSColorFromRGB(0x7a7a7a);
        [self.contentView addSubview:timeLabel];
        self.timeLabel = timeLabel;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    /**
     头像
     */
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.contentView).offset(10);
        
        make.top.mas_equalTo(self.contentView).offset(30);
        
        make.width.height.mas_equalTo(40);
        
        //make.bottom.mas_equalTo(self.contentView);
    }];
    
    [self.whiteBgView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(self.headImageView).offset(30);

        make.left.mas_equalTo(self.headImageView.mas_right).offset(10);

        make.bottom.mas_equalTo(self.contentView).offset(-20);
    }];

    [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.left.mas_equalTo(self.whiteBgView).offset(10);

        make.right.bottom.mas_equalTo(self.whiteBgView).offset(-10);
    }];

    [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(self.contentView);

        make.top.mas_equalTo(self.contentView).offset(12);
    }];
}

-(void)setDetailText:(NSString *)detailText{
    
    _detailText = detailText;
    
    if ([detailText rangeOfString:QiNiuBase].location == NSNotFound) {
        
        [self.detailImageView removeFromSuperview];
        
        self.detailImageView = nil;
        
        CGSize titleSize = [detailText sizeWithFont:MidFont maxSize:CGSizeMake(MAXFLOAT, 30)];
        
        [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.whiteBgView).offset(10);
            
            make.left.mas_equalTo(self.whiteBgView).offset(20);
            
            if (titleSize.width >= SCREEN_WIDTH - 90) {
                
                make.width.mas_equalTo(SCREEN_WIDTH - 90);
            }
            else{
                
                make.width.mas_equalTo(titleSize.width + 10);
            }
            
            make.right.bottom.mas_equalTo(self.whiteBgView).offset(-10);
        }];
        
        [self.whiteBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.headImageView).offset(30);
            
            make.left.mas_equalTo(self.headImageView.mas_right).offset(10);
            
            make.bottom.mas_equalTo(self.contentView).offset(-20);
        }];
        
        self.detailLabel.text = detailText;
        
    } else {
        
        [self.detailLabel removeFromSuperview];
        
        [self.detailImageView sd_setImageWithURL:[NSURL URLWithString:detailText] placeholderImage:PlaceHold];
        
        [self.detailImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.left.mas_equalTo(self.whiteBgView);
            
            make.width.height.mas_equalTo(120);
            
            make.bottom.right.mas_equalTo(self.whiteBgView);
        }];
        
        [self.whiteBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.headImageView).offset(30);
            
            make.left.mas_equalTo(self.headImageView.mas_right).offset(10);
            
            make.bottom.mas_equalTo(self.contentView).offset(-20);
        }];
        
        
    }
}

-(void)layoutSubviews{
    
    //创建maskLayer
//    CALayer *maskLayer = [CALayer layer];
    
//    UIImage *maskImage = [UIImage imageNamed:@"icon_talk"];
    
    //给图层的contents添加内容
//    maskLayer.contents = (__bridge id)maskImage.CGImage;
    
    //给maskLayer 设定frame
//    maskLayer.frame = self.whiteBgView.bounds;
    
//    maskLayer.backgroundColor = CSClearColor.CGColor;
    
//    self.detailImageView.layer.mask = maskLayer;
}
@end
